/***************************************************************************
 创建者：华磊
 开始时间：         : 2020.12.08
 copyright            : (C) 深圳市华友高科有限公司
 修改说明：（每次有修改就添加一条，带有 修改人，修改时间，修改描述）

 ***************************************************************************
 *   运动指令属性编辑视窗
*                                                                         *
 ***************************************************************************/
#include "motioninstruction2view.h"
#include "ui_motioninstruction2view.h"
#include "motioninstructionview.h"
#include "../../programView/programview.h"

MotionInstruction2View::MotionInstruction2View(int styleId,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MotionInstruction2View)
{
    ui->setupUi(this);

    this->setGeometry(5,50,431,544);

    initialDisplayStyle(styleId);
}

void MotionInstruction2View::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        ui->note_GBox->hide();
        ui->forward_Btn->setGeometry(10,350,70,40);
        ui->save_Btn->setGeometry(360,350,70,40);
        break;
    default:
        break;
    }
}

MotionInstruction2View::~MotionInstruction2View()
{
    delete ui;
}

void MotionInstruction2View::updateView()
{
    //先判断是普通的运动还是冲压专用的运动
    if(EM_INSTRUCTION_NORMAL == motionInstructionView->instructionMainFlag)
    {
        ui->fixedType_comboBox->setEnabled(true);

        ui->DI_label_2->show();
        ui->lblIndex_lineEdit->show();

        ui->Wjnt_checkBox->show();
        ui->INC_checkBox->show();
        ui->ACC_checkBox->show();
        ui->ACC_slider->show();
        ui->accValue_lineEdit->show();
        ui->EV_checkBox->show();
        ui->EV_slider->show();
        ui->label_3->show();
        ui->evValue_lineEdit->show();
        ui->PTH_checkBox->show();
        ui->Offset_checkBox->show();
        ui->Offset_PRData_lineEdit->show();
        ui->Tool_Offset_checkBox->show();
        ui->ToolOffset_PRData_lineEdit->show();
    }
    else
    {
        ui->fixedType_comboBox->setEnabled(false);

        ui->DI_label_2->hide();
        ui->lblIndex_lineEdit->hide();

        ui->Wjnt_checkBox->hide();
        ui->INC_checkBox->hide();
        ui->ACC_checkBox->hide();
        ui->ACC_slider->hide();
        ui->accValue_lineEdit->hide();
        ui->EV_checkBox->hide();
        ui->EV_slider->hide();
        ui->label_3->hide();
        ui->evValue_lineEdit->hide();
        ui->PTH_checkBox->hide();
        ui->Offset_checkBox->hide();
        ui->Offset_PRData_lineEdit->hide();
        ui->Tool_Offset_checkBox->hide();
        ui->ToolOffset_PRData_lineEdit->hide();
    }

    //原
    ui->fixedType_comboBox->setCurrentIndex(motionInstructionView->fixedPositionTypeFlag);

    if(0 == motionInstructionView->fixedPositionTypeFlag)
    {
        ui->CNT_slider->setEnabled(false);
        ui->CNT_lineEdit->setEnabled(false);
    }
    else if(1 == motionInstructionView->fixedPositionTypeFlag)
    {
        ui->CNT_slider->setEnabled(true);
        ui->CNT_lineEdit->setEnabled(true);
        ui->CNT_lineEdit->setText(QString::number(motionInstructionView->cntValue));
        ui->CNT_slider->setValue(motionInstructionView->cntValue);
    }

    if(EM_INSTRUCTION_NORMAL == motionInstructionView->instructionMainFlag)
    {
        if(E_MOTION_C == motionInstructionView->instructionTypeFlag)//EM_INSTRUCTION_TYPE_C
        {
            ui->DI_GBox->setEnabled(false);
            ui->Wjnt_checkBox->setEnabled(false);
            ui->INC_checkBox->setEnabled(false);
            ui->ACC_checkBox->setEnabled(false);
            ui->ACC_slider->setEnabled(false);
            ui->accValue_lineEdit->setEnabled(false);
            ui->EV_checkBox->setEnabled(false);
            ui->EV_slider->setEnabled(false);
            ui->label_3->setEnabled(false);
            ui->evValue_lineEdit->setEnabled(false);
            ui->PTH_checkBox->setEnabled(false);
            ui->Offset_checkBox->setEnabled(true);
            ui->Offset_PRData_lineEdit->setEnabled(true);
        }
        else
        {
            ui->DI_GBox->setEnabled(true);
            ui->Wjnt_checkBox->setEnabled(true);
            ui->INC_checkBox->setEnabled(true);
            ui->ACC_checkBox->setEnabled(true);
            ui->ACC_slider->setEnabled(true);
            ui->accValue_lineEdit->setEnabled(true);
            ui->EV_checkBox->setEnabled(true);
            ui->EV_slider->setEnabled(true);
            ui->label_3->setEnabled(true);
            ui->evValue_lineEdit->setEnabled(true);
            ui->PTH_checkBox->setEnabled(true);

            if(1 == motionInstructionView->isUseDiSkip)
            {
                ui->DI_checkBox->setChecked(true);
                ui->DI_linEedit->setText(motionInstructionView->diIndex);
                ui->DISwitch_combox->setCurrentIndex(motionInstructionView->diSwitchFlag);
                ui->lblIndex_lineEdit->setText(motionInstructionView->labelIndex);
            }
            else
            {
                ui->DI_checkBox->setChecked(false);
                ui->DI_linEedit->clear();
            }

            if(motionInstructionView->wjntFlag == 1)
            {
                ui->Wjnt_checkBox->setChecked(true);
            }
            else
            {
                ui->Wjnt_checkBox->setChecked(false);
            }
            if(motionInstructionView->incFlag == 1)
            {
                ui->INC_checkBox->setChecked(true);
            }
            else
            {
                ui->INC_checkBox->setChecked(false);
            }
            if(motionInstructionView->accFlag == 1)
            {
                ui->ACC_checkBox->setChecked(true);
                ui->accValue_lineEdit->setText(QString::number(motionInstructionView->accValue));
                ui->ACC_slider->setValue(motionInstructionView->accValue);
            }
            else
            {
                ui->ACC_checkBox->setChecked(false);
                ui->accValue_lineEdit->clear();
            }
            if(motionInstructionView->evFlag == 1)
            {
                ui->EV_checkBox->setChecked(true);
                ui->evValue_lineEdit->setText(QString::number(motionInstructionView->evValue));
                ui->EV_slider->setValue(motionInstructionView->evValue);
            }
            else
            {
                ui->EV_checkBox->setChecked(false);
                ui->evValue_lineEdit->clear();
            }
            if(motionInstructionView->pthFlag == 1)
            {
                ui->PTH_checkBox->setChecked(true);
            }
            else
            {
                ui->PTH_checkBox->setChecked(false);
            }
        }

        if(motionInstructionView->offsetFlag == 1)
        {
            ui->Offset_checkBox->setChecked(true);
            ui->Offset_PRData_lineEdit->setText(motionInstructionView->offsetPrIndex);
        }
        else
        {
            ui->Offset_checkBox->setChecked(false);
        }

        if(motionInstructionView->toolOffsetFlag == 1)
        {
            ui->Tool_Offset_checkBox->setChecked(true);
            ui->ToolOffset_PRData_lineEdit->setText(motionInstructionView->toolOffsetPrIndex);
        }
        else
        {
            ui->Tool_Offset_checkBox->setChecked(false);
        }

    }
    else//2017.05.16补充:支持冲压的格式
    {
        ui->DI_GBox->setEnabled(true);
        ui->Wjnt_checkBox->setEnabled(true);
        ui->INC_checkBox->setEnabled(true);
        ui->ACC_checkBox->setEnabled(true);
        ui->ACC_slider->setEnabled(true);
        ui->accValue_lineEdit->setEnabled(true);
        ui->EV_checkBox->setEnabled(true);
        ui->EV_slider->setEnabled(true);
        ui->label_3->setEnabled(true);
        ui->evValue_lineEdit->setEnabled(true);
        ui->PTH_checkBox->setEnabled(true);

        //
        if(1 == motionInstructionView->isUseDiSkip)
        {
            ui->DI_checkBox->setChecked(true);
        }
        else
        {
            ui->DI_checkBox->setChecked(false);
            ui->DI_linEedit->clear();
        }

        ui->DI_linEedit->setText(motionInstructionView->diIndex);
        ui->DISwitch_combox->setCurrentIndex(motionInstructionView->diSwitchFlag);
    }

    ui->note_lineEdit->setText(motionInstructionView->moIns.comment);
}

void MotionInstruction2View::getInput()
{
    motionInstructionView->instructionFixedPositionType = ui->fixedType_comboBox->currentText();
    motionInstructionView->cntValue = ui->CNT_lineEdit->text().toInt();
    motionInstructionView->diIndex = ui->DI_linEedit->text();
    motionInstructionView->diSwitch = ui->DISwitch_combox->currentText();
    motionInstructionView->labelIndex = ui->lblIndex_lineEdit->text();
    motionInstructionView->moIns.comment = ui->note_lineEdit->text();

    motionInstructionView->accValue = ui->accValue_lineEdit->text().toInt();
    motionInstructionView->evValue = ui->evValue_lineEdit->text().toInt();
    motionInstructionView->offsetPrIndex = ui->Offset_PRData_lineEdit->text();
    motionInstructionView->toolOffsetPrIndex = ui->ToolOffset_PRData_lineEdit->text();
}

void MotionInstruction2View::changeLanguage()
{
    ui->retranslateUi(this);
}

void MotionInstruction2View::on_forward_Btn_clicked()
{
    this->close();
    motionInstructionView->show();
}

void MotionInstruction2View::on_save_Btn_clicked()
{
    motionInstructionView->getInput();

    emit finishEditInstruction();

    if(E_MOTION_C == motionInstructionView->instructionTypeFlag)
    {
        emit saveInstruction(motionInstructionView->cInstruction);
    }
    else
    {
         emit saveInstruction(motionInstructionView->motionInstruction);
    }
}

void MotionInstruction2View::on_CNT_slider_sliderMoved(int position)
{
    motionInstructionView->cntValue = position;
    ui->CNT_lineEdit->setText(QString::number(motionInstructionView->cntValue));
}


void MotionInstruction2View::on_clearNote_Btn_clicked()
{
    ui->note_lineEdit->clear();
    emit cleanNote();
}

void MotionInstruction2View::on_Wjnt_checkBox_clicked()
{
    emit selectWjntInstruction();
}

void MotionInstruction2View::on_INC_checkBox_clicked()
{
    emit selectIncInstruction();
}

void MotionInstruction2View::on_ACC_checkBox_clicked()
{
    if(ui->ACC_checkBox->isChecked())
    {
        ui->accValue_lineEdit->setText(QString::number(ui->ACC_slider->value()));
    }

    emit selectAccInstruction();
}

void MotionInstruction2View::on_Offset_checkBox_clicked()
{
    emit selectOffsetInstruction();
}

void MotionInstruction2View::on_Tool_Offset_checkBox_clicked()
{
    emit selectToolOffsetInstruction();
}

void MotionInstruction2View::on_EV_checkBox_clicked()
{
    emit selectEvInstruction();
}

void MotionInstruction2View::on_PTH_checkBox_clicked()
{
    emit selectPthInstruction();
}

void MotionInstruction2View::on_ACC_slider_sliderMoved(int position)
{
    motionInstructionView->accValue = position;
    ui->accValue_lineEdit->setText(QString::number(motionInstructionView->accValue));
}

void MotionInstruction2View::on_EV_slider_sliderMoved(int position)
{
    motionInstructionView->evValue = position;
    ui->evValue_lineEdit->setText(QString::number(motionInstructionView->evValue));
}

void MotionInstruction2View::on_CNT_slider_valueChanged(int value)
{
    motionInstructionView->cntValue = value;
    ui->CNT_lineEdit->setText(QString::number(motionInstructionView->cntValue));
}

void MotionInstruction2View::on_ACC_slider_valueChanged(int value)
{
    motionInstructionView->accValue = value;
    ui->accValue_lineEdit->setText(QString::number(motionInstructionView->accValue));
}

void MotionInstruction2View::on_EV_slider_valueChanged(int value)
{
    motionInstructionView->evValue = value;
    ui->evValue_lineEdit->setText(QString::number(motionInstructionView->evValue));
}

void MotionInstruction2View::on_fixedType_comboBox_currentIndexChanged(int index)
{
    motionInstructionView->fixedPositionTypeFlag = index;

    if(0 == motionInstructionView->fixedPositionTypeFlag)
    {
        ui->CNT_slider->setEnabled(false);
        ui->CNT_lineEdit->setEnabled(false);
    }
    else if(1 == motionInstructionView->fixedPositionTypeFlag)
    {
        ui->CNT_slider->setEnabled(true);
        ui->CNT_lineEdit->setEnabled(true);
        motionInstructionView->cntValue=50;
        ui->CNT_lineEdit->setText(QString::number(motionInstructionView->cntValue));
        ui->CNT_slider->setValue(motionInstructionView->cntValue);
    }
}

void MotionInstruction2View::on_DI_checkBox_clicked(bool checked)
{

     emit selectDiInstruction_signal(checked);


}
